﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.TabPageControls
{
    public partial class LapPhieuMuaHang : UserControl
    {
        public LapPhieuMuaHang()
        {
            InitializeComponent();
        }

        private void LapPhieuDeNghiMuaHang2_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();

            // Fill datagridview phieu mua hang
            dgvPhieuMH.Rows.Clear();
            var listPhieuMH = from qt in dc.QuyTrinhMuaHangs
                              where qt.deleted == false &&
                                    qt.PhieuMuaHang != null
                              orderby qt.PhieuMuaHang.NgayTao descending
                              select qt.PhieuMuaHang;

            if (!Program.NguoiDungHienTai.coQuyen(37))
                listPhieuMH = listPhieuMH.Where(x => x.MaNguoiTao == Program.MaNguoiDungHienTai);

            int count = 1;
            foreach (_DB.PhieuMuaHang p in listPhieuMH)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(dgvPhieuMH);
                row.Cells[0].Value = count++;
                row.Cells[1].Value = p.MaPhieu;
                row.Cells[2].Value = p.NguoiTao.Ten;
                row.Cells[3].Value = p.NgayTao;
                row.Cells[4].Value = p.QuyTrinhMuaHangs.Single().PhieuDeNghiMuaHang.PhieuDeNghiMuaHang_HangHoas.Count;
                row.Cells[5].Value = p.QuyTrinhMuaHangs.Single().TrangThaiQuyTrinhMuaHang.MoTa;
                row.Tag = p.QuyTrinhMuaHangs.Single().TrangThai;

                dgvPhieuMH.Rows.Add(row);
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            OtherForms.ThemPhieuMuaHang f = new OtherForms.ThemPhieuMuaHang();
            f.ShowDialog();
            loadData();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            // check
            if (dgvPhieuMH.SelectedRows.Count <= 0)
                return;

            int trangthai = (int)dgvPhieuMH.SelectedRows[0].Tag;
            if (trangthai != _DB.TrangThaiQuyTrinhMuaHang.DA_TAO_PHIEU_MUA_HANG)
                return;

            // go
            string maPhieu = (string) dgvPhieuMH.SelectedRows[0].Cells[1].Value;
            //OtherForms.SuaPhieuMuaHang f = new OtherForms.SuaPhieuMuaHang(maPhieu);
            OtherForms.ThemPhieuMuaHang f = new OtherForms.ThemPhieuMuaHang(maPhieu);
            f.ShowDialog();
        }

        private void dgvPhieuMH_DoubleClick(object sender, EventArgs e)
        {
            if (dgvPhieuMH.SelectedRows.Count > 0)
            {
                string maPhieuMH = (string)dgvPhieuMH.SelectedRows[0].Cells[1].Value;
                OtherForms.XemPhieuMuaHang f = new OtherForms.XemPhieuMuaHang(maPhieuMH);
                f.Show();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // check
            if (dgvPhieuMH.SelectedRows.Count <= 0)
                return;

            int trangthai = (int)dgvPhieuMH.SelectedRows[0].Tag;
            if (trangthai != _DB.TrangThaiQuyTrinhMuaHang.DA_TAO_PHIEU_MUA_HANG)
                return;

            if (MessageBox.Show("Chắc chắn", "", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            // update
            string maPhieu = (string)dgvPhieuMH.SelectedRows[0].Cells[1].Value;
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.QuyTrinhMuaHang qt = dc.QuyTrinhMuaHangs.Single(x => x.MaPhieuMuaHang == maPhieu);
            qt.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.DANG_MUA_HANG;
            qt.PhieuMuaHang.MaNguoiDuyet = Program.MaNguoiDungHienTai;
            qt.PhieuMuaHang.NgayDuyet = DateTime.Now;
            dc.SubmitChanges();
            loadData();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // check
            if (dgvPhieuMH.SelectedRows.Count <= 0)
                return;

            int trangthai = (int)dgvPhieuMH.SelectedRows[0].Tag;
            if (trangthai != _DB.TrangThaiQuyTrinhMuaHang.DA_TAO_PHIEU_MUA_HANG)
                return;

            if (MessageBox.Show("Chắc chắn", "", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            // update
            string maPhieu = (string)dgvPhieuMH.SelectedRows[0].Cells[1].Value;
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.QuyTrinhMuaHang qt = dc.QuyTrinhMuaHangs.Single(x => x.MaPhieuMuaHang == maPhieu);
            _DB.PhieuMuaHang p = dc.PhieuMuaHangs.Single(x => x.MaPhieu == maPhieu);
            p.deleted = true;
            qt.MaPhieuMuaHang = null;
            qt.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.DA_DUYET_PHIEU_DE_NGHI;
            dc.SubmitChanges();
            loadData();
        }
    }
}
